<?php if(!defined('SPRITCH')) { exit(); }

// Spritch - An opensource PHP5 web framework

// Author: Luca Degasperi
// Licence: GNU GPL
// Site: http://www.spritch.org/

// Class: URI
// Summary: This class provides URI segmentation functions
// Since: 0.9.1

final class uri extends spritch
{
	private $uri = array();
	private $language;
	
	public function __construct()
	{
		$this->initialize();
		$this->profiler->log('uri class started');
	}
	
	private function initialize()
	{
		$url = str_replace('index.php','',$_SERVER['SCRIPT_NAME']);
		$this->uri['url'] = 'http://'.$_SERVER['HTTP_HOST'].$url;
		
		file_exists('.htaccess') ? $this->uri['base'] = $url : $this->uri['base'] = $_SERVER['SCRIPT_NAME'].DS;
		
		$rawuri = $this->get_rawuri();
		
		$module = APPLICATION;
		if(!empty($rawuri) and file_exists(MODULES.DS.$rawuri[0]) and !file_exists(APPLICATION.DS.'controllers'.DS.$rawuri[0]) and !file_exists(APPLICATION.DS.'languages'.DS.$rawuri[0]))
		{
			$module = MODULES.DS.$rawuri[0];
			array_splice($rawuri,0,1);
		}
		$this->uri['module'] = $module;

		define('MODULE',         $module);
		define('MODELS',         $module.DS.'models');
		define('VIEWS',          $module.DS.'views');
		define('CONFIGURATION',  $module.DS.'configuration');
		define('CONTROLLERS',    $module.DS.'controllers');
		define('HOOKS',          $module.DS.'hooks');
		define('HELPERS',        $module.DS.'helpers');
		define('LANGUAGES',      $module.DS.'languages');
		define('LIBRARIES',      $module.DS.'libraries');
		define('PLUGINS',        $module.DS.'plugins');
		define('WEBACCESS',      $module.DS.'webaccess');
		define('IMAGES',         $module.DS.'webaccess'.DS.'images');
		define('SCRIPTS',        $module.DS.'webaccess'.DS.'scripts');
		define('STYLES',         $module.DS.'webaccess'.DS.'styles');

		$language = $this->configuration->get('language','en');
		if($this->configuration->get('multilang',false))
			if(file_exists(LANGUAGES.DS.$rawuri[0]))
			{
				$language = $rawuri[0];
				$this->language = $language.DS;
				array_shift($rawuri);
			}
		$this->uri['language'] = $language;
		
		$root = '';
		$controller = '404';
		$i = 0;
		$file = false;
		$path = '';
		while(!$file and $i < count($rawuri))
		{
			if($i == 0)
				$tmp = $rawuri[$i++];
			else
				$tmp .= DS.$rawuri[$i++];
			
			$path = $this->exists($tmp,'controllers');
			if(!empty($path))
				$file = true;
			else
				$root = $tmp;		
		}
		if(count($rawuri) == 0)
			$controller = $this->configuration->get('default_page','home');
		if($file)
		{
			$path = str_replace(EXT,'',$path);
			$patharr = explode('/',$path);
			$controller = $patharr[count($patharr)-1];
			array_splice($rawuri,0,$i);
		}
		$this->uri['root'] = $root;
		$this->uri['controller'] = $controller;

		if(!empty($rawuri[0]) and $rawuri[0] != '__construct' and $this->uri['controller'] != '404')
		{
			$this->uri['action'] = $rawuri[0];
			array_splice($rawuri,0,1);
		}
		else
			$this->uri['action'] = 'index';

		$args = array();
		foreach($rawuri as $segment)
			array_push($args,$segment);
		$this->uri['args'] = $args;
	}
	
	private function get_rawuri()
	{
		$uri = $this->uri_string();
		$uri = rawurldecode($uri);
		$uri = strtolower($uri);
		
		$uri = substr($uri,1);
		if(!$uri)
			$uri = '';
		else
			if(substr($uri,count($uri)-2,1) == '/')
				$uri = substr($uri,0,count($uri)-2);
		
		$uri_arr = array();
		if(!empty($uri))
			$uri_arr = explode('/',$uri);
						
		return $uri_arr;
	}
	
	public function uri_string()
	{
		return isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
	}
	
	public function routed_string()
	{
		$string = '';
		foreach($this->uri as $key => $value)
		{
			if(is_array($value))
			{
				$tmp = '';
				foreach($value as $value2)
					$tmp .= '/'.$value2;
				$tmp .= '/';
				$tmp = str_replace('//','/',$tmp);
				$string .= $tmp;
			}
			else
			{
				$string .= '/'.$value;
			}
		}
		return $string;
	}
	
	public function url() { return $this->uri['url']; }
	
	public function base($language = true) { if($language) return $this->uri['base'].$this->language; return $this->uri['base']; }

	public function language() { return $this->uri['language']; }
	
	public function root() { return $this->uri['root']; }
	
	public function controller() { return $this->uri['controller']; }
	
	public function action() { return $this->uri['action']; }
	
	public function args() { return $this->uri['args']; }
}
?>